require(['./config'], () => {
    require(['header','footer'], () => {
        class Account {
            constructor () {
                // console.log(getDate.changeDate)
                // getDate.changeDate()
                this.testregsiter()
                this.testlogin()
            }
            testregsiter() {
                const uemail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
                $('#uemail').on('blur', function () {
                    if (uemail.test($('#uemail').val()) === true) {
                      $('#uemail').after('<p style="color:green">验证成功√</p>')
                    } else {
                        $('#uemail').after('<p style="color:red">验证失败×</p>')
                    }
                })
                const upwd = /\d{3,20}/
                $('#upwd').on('blur', function () {
                    if (upwd.test($('#upwd').val()) === true) {
                        $('#upwd').after('<p style="color:green">密码格式正确√</p>')
                    } else {
                        $('#upwd').after('<p style="color:red">密码长度为三位数字及以上×</p>')
                    }
                })
                const uphone = /\d{11}/
                $('#uphone').on('blur', function () {
                    if (uphone.test($('#uphone').val()) === true) {
                        $('#uphone').after('<p style="color:green">验证成功√</p>')
                    } else {
                        $('#uphone').after('<p style="color:red">手机号码长度为11位×</p>')
                    }
                })
                $('#register').on('click', function () {
                    if (uemail.test($('#uemail').val()) === true && upwd.test($('#upwd').val()) === true&& uphone.test($('#uphone').val()) === true) {
                        const registeremail = $('#uemail').val()
                        const registerpsd = $('#upwd').val()
                        const registerphone= $('#uphone').val()
                        const userinfo = { registeremail, registerpsd, registerphone }
                        let str = JSON.stringify(userinfo)
                        let users = localStorage.getItem("users")
                        console.log(str);
                        if (users) {
                            users = JSON.parse(users)
                            users.push(userinfo)
                            localStorage.setItem("users", JSON.stringify(users))
                        }
                        else {
                            var arr = [userinfo];
                            localStorage.setItem("users", JSON.stringify(arr))
                        } 
                  
                           alert('注册成功，请登录')

                    }
                })
            }
            testlogin() {
                let users = localStorage.getItem('users')
                console.log(users);

                $('#login').on('click', function () {
                    if (users) {
                        users = JSON.parse(users)
                        const isExist = users.some(user => {
                            return user.registerphone === $('.userphone').val() && user.registerpsd === $('.userpsd').val()

                        })

                        if (isExist) {
                            alert('登录成功，即将跳转首页')
                            window.location.replace('../index.html')
                        } else {
                            $('.userpsd').after('<p style="color:red">账号或密码错误×</p>')
                        }
                    } 
                })

            }
        }
        new Account()
    })

})